package bb.lanxing.mvp.model;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import bb.lanxing.model.json.ServerJson;
import bb.lanxing.model.json.WorkoutComment;
import bb.lanxing.model.json.WorkoutLap;
import bb.lanxing.model.json.WorkoutLike;
import bb.lanxing.mvp.model.i.IWorkoutModel;
//import bb.lanxing.network.BiciHttpClient;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.network.NetSubscribe;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public abstract class BaseWorkoutModel implements IWorkoutModel {
    @Override
    public void workoutLike(long serverId, boolean like, Subscriber<Boolean> subscriber) {
        Observable.create(new NetSubscribe(MyHttpClient.segmentLike(serverId, like))).subscribeOn(Schedulers.io())
                .map(str -> true).observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber);
    }

    @Override
    public void createRouteBook(long workoutId, Subscriber<Long> subscriber) {
        Observable.create(new NetSubscribe(MyHttpClient.createRouteBookByWorkoutId(workoutId)))
                .subscribeOn(Schedulers.io()).map(str -> JSON.parseObject(str).getJSONObject("data").getLong("serverId"))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber);
    }

    @Override
    public Observable<Object[]> getWorkoutComment(long workoutId, int page, int limit) {
        return Observable.create(new NetSubscribe(MyHttpClient.getWorkoutComment(workoutId, page, limit)))
                .subscribeOn(Schedulers.io()).flatMap(str -> {
                    JSONObject ajaxObject = JSON.parseObject(str);
                    JSONObject dataObject = ajaxObject.getJSONObject("data");
                    JSONObject parseObject = JSON.parseObject(dataObject.toString());

                    List<WorkoutComment> comments =
                            JSON.parseArray(parseObject.getString("comments"), WorkoutComment.class);
                    List<WorkoutLike> users_xing =
                            JSON.parseArray(parseObject.getString("users_xing"), WorkoutLike.class);
                    Integer comments_count = parseObject.getInteger("comments_count");
                    Integer xing_count = parseObject.getInteger("xing_count");
                    if (comments_count == null) {
                        comments_count = 0;
                    }
                    if (xing_count == null) {
                        xing_count = 0;
                    }
                    Object[] objArr = new Object[4];
                    if (comments == null) {
                        comments = Collections.EMPTY_LIST;
                    }
                    objArr[0] = comments;
                    objArr[1] = users_xing;
                    objArr[2] = comments_count;
                    objArr[3] = xing_count;
                    return Observable.just(objArr);
                });
    }

    @Override
    public void workoutComment(long workoutId, String content, String photoUrl, long informedUserId,
            Subscriber<Boolean> subscriber) {
        Observable
                .create(new NetSubscribe(MyHttpClient.setWorkoutComment(workoutId, content, photoUrl, informedUserId)))
                .subscribeOn(Schedulers.io()).flatMap(str3 -> Observable.just(true))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber);
    }

    @Override
    public void getWorkoutLap(long j, Subscriber<Map<Long, List<WorkoutLap>>> subscriber) {
        Observable.create(new NetSubscribe(MyHttpClient.getWorkoutLap(j, -1L))).subscribeOn(Schedulers.io())
                .flatMap(str -> {
                    JSONArray parseArray =
                            JSON.parseArray(((ServerJson) JSON.parseObject(str, ServerJson.class)).getData());
                    HashMap<Long, List<WorkoutLap>> hashMap = new HashMap<>();
                    if (parseArray != null) {
                        for (Object o : parseArray) {
                            JSONObject parseObject = JSON.parseObject(o.toString());
                            hashMap.put(parseObject.getJSONObject("lushu_info").getLong("id"),
                                    JSON.parseArray(parseObject.getString("segments"), WorkoutLap.class));
                        }
                    }
                    return Observable.just(hashMap);
                }).observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber);
    }
}
